from django.db import models
from utils.basemodel import BaseModel


# 多分表分库
# 分类
class Tag(BaseModel):
    # CharField必须要指定max_length
    name = models.CharField(max_length=80, verbose_name="分类名称")

    class Meta:
        db_table = "tb_tag"
        verbose_name = "分类"
        verbose_name_plural = verbose_name
        app_label = "news"

    def __str__(self):
        return f"Tag({self.name})"


# 新闻
class News(BaseModel):
    title = models.CharField(max_length=150, verbose_name="文章标题")
    digest = models.CharField(max_length=200, verbose_name="文章摘要")
    content = models.TextField(verbose_name="文章内容")
    clicks = models.IntegerField(default=0, verbose_name="点击量")
    image_url = models.URLField(default="", verbose_name="图片链接")
    tag = models.ForeignKey("Tag", on_delete=models.SET_NULL, null=True)
    # 如果是其他模块的数据库，关联需要使用app_name.model_name
    author = models.ForeignKey("users.User", on_delete=models.SET_NULL, null=True)

    class Meta:
        # 默认排序
        ordering = ["-update_time", "-id"]
        # 指明数据库名
        db_table = "tb_news"
        verbose_name = "新闻"
        verbose_name_plural = verbose_name

    def increase_click(self):
        self.clicks += 1
        self.save(update_fields=['clicks'])

    def __str__(self):
        return f"News({self.id}, {self.title})"


# 评论
class Comment(BaseModel):
    content = models.TextField(verbose_name="评论内容")
    author = models.ForeignKey("users.User", on_delete=models.CASCADE, verbose_name="评论人")
    news = models.ForeignKey("News", on_delete=models.CASCADE, verbose_name="评论的新闻")
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True)

    class Meta:
        # 默认排序
        ordering = ["-update_time", "-id"]
        # 指明数据库名
        db_table = "tb_comment"
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"Comment({self.id}, {self.author})"


# 热门新闻
class HotNews(BaseModel):
    news = models.OneToOneField("News", on_delete=models.CASCADE, verbose_name="热门文章")
    choices = [
        (1, "最高级"),
        (2, "高级"),
        (3, "中级"),
        (4, "低级"),
        (5, "最低级"),
    ]
    priority = models.IntegerField(verbose_name="优先级", choices=choices)

    class Meta:
        # 默认排序
        ordering = ["-update_time", "-id"]
        # 指明数据库名
        db_table = "tb_hotnews"
        verbose_name = "热门新闻"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"HotNews({self.id}, {self.news.title})"


# 轮播图
class Banner(BaseModel):
    image_url = models.URLField(default="", verbose_name="轮播图链接")
    choices = [
        (1, "最高级"),
        (2, "高级"),
        (3, "中级"),
        (4, "低级"),
        (5, "最低级"),
    ]
    priority = models.IntegerField(verbose_name="优先级", choices=choices)
    news = models.OneToOneField("News", on_delete=models.CASCADE)

    class Meta:
        # 默认排序
        ordering = ["-update_time", "-id"]
        # 指明数据库名
        db_table = "tb_banner"
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"Banner({self.id}, {self.image_url})"



